package com.hisense.ovcloud.acjulink.tools;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONUtil;
import com.hisense.ovcloud.acjulink.dto.CheckTokenDTO;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class PayloadTool {
    public String sha256Encode(String str) {
        byte[] originSha256ByteArray = DigestUtil.sha256Hex(str).getBytes(StandardCharsets.UTF_8);
        if (originSha256ByteArray.length > 64) {
            return new String(Arrays.copyOfRange(originSha256ByteArray, 0, 64));
        } else {
            return new String(originSha256ByteArray);
        }
    }

    public boolean checkSha256Msg(String msg) {
        int jsonEndIndex = msg.length() - 64 - 2;
        String jsonMsg = StrUtil.sub(msg, 2, jsonEndIndex);
        String sha256Sign = StrUtil.sub(msg, jsonEndIndex, msg.length() - 2);
        return sha256Encode(jsonMsg).equals(sha256Sign);
    }

    public boolean checkToken(String msgJsonPayload) {
        CheckTokenDTO checkToken = JSONUtil.toBean(msgJsonPayload, CheckTokenDTO.class);
        return false;
    }
}
